package com.hui.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hui.entity.*;
import com.hui.mapper.UserMapper;
import com.hui.utils.Md5;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: 李昭辉
 * @Date: 2021/08/07/16:07
 * @Description:
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public PageInfo<User> getUserByCondition(UserVo userVo) {
        // 设置分页
        PageHelper.startPage(userVo.getCurrentPage(), userVo.getPageSize());
        // 查询数据
        List<User> list = userMapper.getUserByCondition(userVo);
        // 将结果封装到 pageInfo 中
        PageInfo<User> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    @Override
    public void updateUserStatus(Integer id, String status) {
        // 封装数据
        User user = new User();
        user.setId(id);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        // 调用dao层
        userMapper.updateUserStatus(user);
    }

    @Override
    public User login(User user) throws Exception {
        User u = userMapper.getUserByPhone(user.getPhone());
        // 查询出当前用户的密码，用md5经行校验
        if (u != null && Md5.verify(user.getPassword(), Md5.md5key, u.getPassword())) {
            return u;
        }
        return null;
    }

    @Override
    public List<Role> getUserRoleById(Integer userId) {
        return userMapper.getUserRoleById(userId);
    }

    @Override
    public void userContextRole(UserVo userVo) {
        // 删除原有的角色
        userMapper.deleteUserRole(userVo.getUserId());
        // 添加用过户与角色关系
        Date date = new Date();
        for (Integer roleId : userVo.getRoleIdList()) {
            // 封装&补全信息
            User_Role_relation relation = new User_Role_relation();
            relation.setUserId(userVo.getUserId());
            relation.setRoleId(roleId);
            relation.setCreatedTime(date);
            relation.setUpdatedTime(date);
            relation.setCreatedBy("system");
            relation.setUpdatedby("system");
            // 执行保存操作
            userMapper.addUserRole(relation);
        }
    }

    @Override
    public Map<String, Object> getUserPermissions(Integer userId) {
        // 根据userId获取角色信息
        List<Role> roleList = userMapper.getUserRoleById(userId);
        if(roleList==null){
            return null;
        }
        // 提取出roleId
        List<Integer> ids = new ArrayList<>();
        for (Role role : roleList) {
            ids.add(role.getId());
        }
        // =======根据ids获取菜单信息=======
        // 1.获取父级菜单
        List<Menu> parentMenu = userMapper.getParentMenuByRoleIds(ids);
        // 2.根据父级菜单id获取子级菜单数据
        for (Menu menu : parentMenu) {
            List<Menu> childrenMenu = userMapper.getMenuByPid(menu.getId());
            menu.setSubMenuList(childrenMenu);
        }
        // =======根据ids获取资源信息=======
        List<Resource> resourceList = userMapper.getResourceByRoleIds(ids);
        // ========封装数据===========
        Map<String, Object> map = new HashMap<>();
        map.put("menuList", parentMenu);
        map.put("resourceList", resourceList);
        return map;
    }
}
